<template>
  <div class="releases">
    <div class="appPc" v-if="$store.state.screenWidth > 1300">
      <div class="backdrop">
        <img
          src="https://ir3.anbio.xyz/uploads/image/6528681748657426.jpg"
          alt=""
        />
        <h1 class="title">Press Releases</h1>
      </div>

      <div class="table releasesInterval">
        <div class="select">
          <el-select
            :popper-append-to-body="false"
            class="device-select"
            style="width: 300px"
            v-model="tableValue"
            placeholder="请选择"
            @change="tabChange"
            clearable
          >
            <el-option
              v-for="item in tableOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>

        <div class="block" v-loading="newLoadshow">
          <div
            class="list"
            v-for="(item, index) in newList"
            :key="index"
            @click="newClick(item)"
          >
            <h2 class="title">
              {{ item.newTitle }}
            </h2>
            <div class="block">
              <div class="time">{{ item.createTime }}</div>
              <div class="pdf" @click.stop="pdfStop(item)">
                <img src="../../assets/common/pdf-file_2.svg" alt="" />
              </div>
            </div>
            <div class="textc">
              {{ item.newDesc }}
            </div>
          </div>

          <div class="pagination">
            <el-pagination
              @current-change="handleCurrentChange"
              :current-page="pageNo"
              :page-size="limit"
              layout="prev, pager, next"
              :total="10"
            >
            </el-pagination>

            <div class="quantity">
              <div
                class="num"
                v-for="(item, index) in 3"
                :key="index"
                @click="paginationMet(index)"
                :class="{ 'num-active': paginationActive == index }"
              >
                {{
                  index == 0 ? "10" : index == 1 ? "25" : index == 2 ? "50" : ""
                }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="appH5" v-else>
      <div class="popup-lint"></div>

      <div class="backdrop">
        <img
          src="https://ir3.anbio.xyz/uploads/image/6528681748657426.jpg"
          alt=""
        />
        <h1 class="title">Press Releases</h1>
      </div>

      <div class="table">
        <div class="select" @click="selectMet">
          <van-cell-group>
            <van-field
              v-model="tableValue"
              placeholder="请输入用户名"
              clearable
              readonly
            />
          </van-cell-group>
        </div>

        <div class="block" v-loading="newLoadshow">
          <div
            class="list"
            v-for="(item, index) in newList"
            :key="index"
            @click="newClick(item)"
          >
            <h2 class="title">
              {{ item.newTitle }}
            </h2>
            <div class="block">
              <div class="time">{{ item.createTime }}</div>
              <div class="pdf" @click.stop="pdfStop(item)">
                <img src="../../assets/common/pdf-file_2.svg" alt="" />
              </div>
            </div>
            <div class="textc">
              {{ item.newDesc }}
            </div>
          </div>

          <div class="pagination">
            <div class="left">
              <el-pagination
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-size="limit"
                layout="prev, pager, next"
                :total="totalNum"
                :pagerCount="5"
              >
              </el-pagination>
            </div>

            <div class="right">
              <div
                class="num"
                v-for="(item, index) in 3"
                :key="index"
                @click="paginationMet(index)"
                :class="{ 'num-active': paginationActive == index }"
              >
                {{
                  index == 0 ? "10" : index == 1 ? "25" : index == 2 ? "50" : ""
                }}
              </div>
            </div>
          </div>
        </div>
      </div>

      <van-popup
        v-model="pickerShow"
        position="bottom"
        style="min-height: 100px"
      >
        <van-picker
          show-toolbar
          :columns="tableOptions"
          value-key="label"
          @cancel="pickerCancel"
          @confirm="pickerConfirm"
        />
      </van-popup>
    </div>
  </div>
</template>

<script>
import { newListGet, newDetailGet } from "../../api/index";
//----------
import html2pdf from 'html2pdf.js';

import pdfHtml from "@/components/common/pdf.vue";

export default {
  components: {
    pdfHtml,
  },
  data() {
    return {
      tableOptions: [
        {
          value: "0",
          label: "All Years",
        },
        {
          value: "2025",
          label: "2025",
        },
        {
          value: "2024",
          label: "2024",
        },
      ],
      tableValue: "",
      pageNo: 1,
      limit: 10,
      totalNum: 0,
      paginationActive: 0,
      //--------------------
      pickerShow: false,
      //-------------------
      newList: [],
      newLoadshow: true,
    };
  },
  created() {
    this.newListGetMet();
    this.scrollTotap();
  },
  methods: {
    scrollTotap() {
      window.scrollTo(0, 0);
    },
    newListGetMet() {
      let params = {
        year: this.tableValue == "All Years" ? "" : this.tableValue,
        newTitle: "",
        pageSize: this.limit,
        pageNo: this.pageNo,
      };

      newListGet(params).then((res) => {
        if (res.errorCode == null) {
          this.newList = res.resultData.newList;
          this.totalNum = res.resultData.totalNum;
          this.newLoadshow = false;
        }
      });
    },
    tabChange(e) {
      this.tableValue = e;
      this.newLoadshow = true;
      this.newListGetMet();
    },

    //----------------------------
    handleCurrentChange(val) {
      this.pageNo = val;
      this.newLoadshow = true;
      this.newListGetMet();
    },
    paginationMet(index) {
      this.paginationActive = index;
      this.limit = index == 0 ? 10 : index == 1 ? 25 : index == 2 ? 50 : "";
      this.newLoadshow = true;
      this.newListGetMet();
    },

    //--------H5
    selectMet() {
      this.pickerShow = !this.pickerShow;
    },
    pickerConfirm(item, index) {
      this.tableValue = item.label;
      this.newLoadshow = true;
      this.pickerShow = false;
      this.newListGetMet();
    },
    pickerCancel() {
      this.pickerShow = false;
      this.tableValue = "";
      this.newLoadshow = true;
      this.newListGetMet();
    },

    //-------
    newClick(item) {
      this.$router.push({
        path: `/releases/${item.newId}`,
      });
    },
    //---------
    pdfStop(item) {
      newDetailGet(item.newId).then((res) => {
        if (res.errorCode == null) {
          this.generatePDF(res.resultData);
        }
      });
    },

    generatePDF(item) {
      const element = document.createElement("div");
      element.innerHTML = item.newContent;
      html2pdf(element, {
        margin: 0.5,
        filename: "report.pdf",
        image: { type: "jpeg", quality: 0.98 },
        html2canvas: { scale: 2 },
        jsPDF: { unit: "in", format: "letter", orientation: "portrait" },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.releases {
  .appPc {
    .backdrop {
      height: 205px;
      width: 100%;
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        position: absolute;
      }

      .title {
        letter-spacing: 1.5px;
        color: #fff;
        position: relative;
        font-size: 48px;
        font-weight: bold;
      }
    }

    .table {
      padding-top: 50px;
      padding-bottom: 50px;
      box-sizing: border-box;

      .select {
        width: 100%;
        margin-bottom: 100px;

        .el-select-dropdown__item.hover,
        .el-select-dropdown__item:hover {
          color: #fff;
          background: #5897fb;
        }
      }

      .block {
        .list {
          padding: 0 0 30px;
          box-sizing: border-box;
          border-bottom: 1px solid #c1c6ce;
          letter-spacing: 0.4px;
          cursor: pointer;
          .title {
            font-size: 28px;
            font-weight: 600;
            color: #2c3341;
            cursor: pointer;
          }

          .block {
            margin-top: 15px;
            display: flex;
            align-items: center;

            .time {
              font-size: 16px;
              font-weight: 500;
              margin-right: 20px;
              color: #838892;
            }

            .pdf {
              width: 30px;
              height: 30px;
              margin-right: 20px;
              cursor: pointer;

              img {
                width: 100%;
                height: 100%;
              }
            }

            .copy {
              width: 30px;
              height: 30px;
              margin-right: 20px;
              border: 1px solid;
              cursor: pointer;
            }
          }

          .textc {
            letter-spacing: 1px;
            margin-top: 15px;
            font-size: 16px;
            font-weight: 500;
            color: #2c3341;
          }
        }

        .list:hover {
          .title {
            color: #5897fb;
          }
        }

        .pagination {
          height: 100px;
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-top: 50px;
          position: relative;

          .quantity {
            height: 40px;
            position: absolute;
            right: 0;
            display: flex;
            align-items: center;

            .num {
              margin-right: 20px;
              font-weight: bold;
              cursor: pointer;
            }

            .num-active {
              color: #5897fb;
            }
          }
        }
      }
    }
  }

  .appH5 {
    .backdrop {
      height: 200px;
      width: 100%;
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        position: absolute;
      }

      .title {
        letter-spacing: 1.5px;
        color: #fff;
        position: relative;
        font-size: 24px;
        font-weight: bold;
      }
    }

    .table {
      padding: 30px 10px;
      box-sizing: border-box;

      .select {
        width: 100%;
        margin-bottom: 60px;

        /deep/ .van-cell {
          border: 1px solid #ccc;
          border-radius: 5px;
          height: 40px;
          line-height: 40px;
          padding: 0;
          padding-left: 10px;
          box-sizing: border-box;
        }
      }

      .block {
        .list {
          padding-bottom: 20px;
          box-sizing: border-box;
          border-bottom: 1px solid #c1c6ce;

          .title {
            font-size: 16px;
            color: #2c3341;
          }

          .block {
            display: flex;
            align-items: center;

            .time {
              font-size: 14px;
              font-weight: 500;
              margin-right: 10px;
            }
            .pdf {
              width: 20px;
              height: 20px;
              margin-right: 10px;

              img {
                width: 100%;
                height: 100%;
              }
            }

            .copy {
              width: 20px;
              height: 20px;
              border: 1px solid;
              margin-right: 10px;
            }
          }

          .textc {
            margin-top: 10px;
            font-size: 14px;
            word-break: break-all;
            text-overflow: ellipsis;
            overflow: hidden;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            font-weight: 500;
          }
        }

        .pagination {
          height: 40px;
          width: 100%;
          display: flex;
          align-items: center;
          margin-top: 50px;
          position: relative;

          .left {
            width: 60%;
          }

          .right {
            width: 40%;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: flex-end;
            margin-left: 20px;

            .num {
              margin-right: 20px;
              font-weight: bold;
              cursor: pointer;
              font-size: 16px;
            }

            .num-active {
              color: #5897fb;
            }
          }
        }
      }
    }
  }

  .indexShow {
    display: none;
    .block {
      padding: 50px 20px;
      box-sizing: border-box;

      .title {
        font-size: 54px;
        font-weight: 500;
      }

      .time {
        font-size: 20px;
        font-weight: 500;
        margin-right: 20px;
        color: #838892;
      }

      .pdf {
        width: 30px;
        height: 30px;
        margin-right: 20px;
        cursor: pointer;

        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
</style>